/*************************************************
*           BK Emulator v3.8 for Windows         *
*************************************************/

/*
File: BrowseForFolder.cpp
Version: 1.0.0
Written by: Yuriy Kalmykov <kalmykov@stoik.com>
    Copyright (c) 2002-2004 Yuriy Kalmykov

    BK Emulator is a program emulated hardware environment for running
code for BK 0010(01) in different configurations.

    This code may be used in compiled form in any way you desire.
This file or it's parts can't be redistributed without the authors
written consent, but can be modified for your private needs.
    Providing that this notice and the authors name and all copyright
notices remains intact.

    Please, an email me to know how you are using it and where. You can
ask me for any information about this below code or any attendant
knowledge.

    This file is provided "as is" with no expressed or implied warranty.
The author accepts no liability for any damage or loss of business that
this product may cause.
*/


//////////////////////////////////////////////////////////////////////
//
// ShellBrowser.cpp: implementation of the CShellBrowser class.
//

#include "stdafx.h"
#include "BrowseForFolder.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
//
// Construction/Destruction
//

CBrowseForFolder::CBrowseForFolder(const HWND hParent /*= NULL*/, const LPITEMIDLIST pidl /*= NULL*/, const int nTitleID /*= 0*/)
{
	m_hwnd = NULL;
	SetOwner(hParent);
	SetRoot(pidl);
	SetTitle(nTitleID);
	m_bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_SHAREABLE | BIF_USENEWUI | BIF_UAHINT;
	m_bi.lpfn = BrowseCallbackProc;
	m_bi.lParam = reinterpret_cast<LONG_PTR>(this);
	m_bi.pszDisplayName = m_szSelected;
}

CBrowseForFolder::CBrowseForFolder(const HWND hParent, const LPITEMIDLIST pidl, const CString &strCurrFolder, const CString &strTitle)
{
	m_hwnd = NULL;
	SetOwner(hParent);
	SetRoot(pidl);
	SetTitle(strTitle);
	m_bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_SHAREABLE | BIF_USENEWUI | BIF_UAHINT;
	m_bi.lpfn = BrowseCallbackProc;
	m_bi.lParam = reinterpret_cast<LONG_PTR>(this);
	m_bi.pszDisplayName = m_szSelected;
	m_strCurrFolder = strCurrFolder;
}

CBrowseForFolder::~CBrowseForFolder()
{
}

//////////////////////////////////////////////////////////////////////
//
// Implementation
//

void CBrowseForFolder::SetOwner(const HWND hwndOwner)
{
	if (m_hwnd != NULL)
	{
		return;
	}

	m_bi.hwndOwner = hwndOwner;
}

void CBrowseForFolder::SetRoot(const LPITEMIDLIST pidl)
{
	if (m_hwnd != NULL)
	{
		return;
	}

	m_bi.pidlRoot = pidl;
}

CString CBrowseForFolder::GetTitle() const
{
	return m_bi.lpszTitle;
}

void CBrowseForFolder::SetTitle(const CString &strTitle)
{
	if (m_hwnd != NULL)
	{
		return;
	}

	size_t len = static_cast<size_t>(strTitle.GetLength()) + 1;
	m_pchTitle = AUTO_STR(new TCHAR [len]);
	StringCchCopy(m_pchTitle.get(), len, strTitle);
	m_bi.lpszTitle = m_pchTitle.get();
}

BOOL CBrowseForFolder::SetTitle(const int nTitle)
{
	if (nTitle <= 0)
	{
		return FALSE;
	}

	CString strTitle;

	if (!strTitle.LoadString(static_cast<size_t>(nTitle)))
	{
		return FALSE;
	}

	SetTitle(strTitle);
	return TRUE;
}

void CBrowseForFolder::SetFlags(const UINT ulFlags)
{
	if (m_hwnd != NULL)
	{
		return;
	}

	m_bi.ulFlags = ulFlags;
}

CString CBrowseForFolder::GetSelectedFolder() const
{
	return m_szSelected;
}

BOOL CBrowseForFolder::SelectFolder()
{
	BOOL bRet = FALSE;
	LPITEMIDLIST pidl;

	if ((pidl = ::SHBrowseForFolder(&m_bi)) != NULL)
	{
		m_strPath.Empty();

		if (::SHGetPathFromIDList(pidl, m_szSelected))
		{
			bRet = TRUE;
			m_strPath = CString(m_szSelected);
		}

		LPMALLOC pMalloc;

		//Retrieve a pointer to the shell's IMalloc interface
		if (SUCCEEDED(SHGetMalloc(&pMalloc)))
		{
			// free the PIDL that SHBrowseForFolder returned to us.
			pMalloc->Free(pidl);
			// release the shell's IMalloc interface
			(void)pMalloc->Release();
		}
	}

	m_hwnd = NULL;
	return bRet;
}

void CBrowseForFolder::OnInit(CWnd *pDlg) const
{
	SetSelection(m_strCurrFolder);
}

void CBrowseForFolder::OnSelChanged(const LPITEMIDLIST pidl) const
{
	(void)pidl;
}

void CBrowseForFolder::EnableOK(const BOOL bEnable) const
{
	if (m_hwnd == NULL)
	{
		return;
	}

	(void)SendMessage(m_hwnd, BFFM_ENABLEOK, static_cast<WPARAM>(bEnable), NULL);
}

void CBrowseForFolder::SetSelection(const LPITEMIDLIST pidl) const
{
	if (m_hwnd == NULL)
	{
		return;
	}

	(void)SendMessage(m_hwnd, BFFM_SETSELECTION, FALSE, reinterpret_cast<long>(pidl));
}

void CBrowseForFolder::SetSelection(const CString &strPath) const
{
	if (m_hwnd == NULL)
	{
		return;
	}

	(void)SendMessage(m_hwnd, BFFM_SETSELECTION, TRUE, reinterpret_cast<long>(LPCTSTR(strPath)));
}

void CBrowseForFolder::SetStatusText(const CString &strText) const
{
	if (m_hwnd == NULL)
	{
		return;
	}

	(void)SendMessage(m_hwnd, BFFM_SETSTATUSTEXT, NULL, reinterpret_cast<long>(LPCTSTR(strText)));
}

int CALLBACK CBrowseForFolder::BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
	TCHAR szPath[_MAX_PATH];
	CBrowseForFolder *pbff = reinterpret_cast<CBrowseForFolder *>(lpData);
	pbff->m_hwnd = hwnd;

	if (uMsg == BFFM_INITIALIZED)
	{
		::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
		pbff->OnInit(CWnd::FromHandle(hwnd));
	}
	else if (uMsg == BFFM_SELCHANGED)
	{
		SHGetPathFromIDList(LPITEMIDLIST(lParam), szPath);
		::SendMessage(hwnd, BFFM_SETSTATUSTEXT, NULL, LPARAM(szPath));
		pbff->OnSelChanged(reinterpret_cast<LPITEMIDLIST>(lParam));
	}

	return 0;
}
